package com.example.t1.ui.main;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.t1.Chessboard;
import com.example.t1.R;

import java.io.FileOutputStream;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    private PageViewModel pageViewModel;

    public static PlaceholderFragment newInstance(int index) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
        int index = 1;
        if (getArguments() != null) {
            index = getArguments().getInt(ARG_SECTION_NUMBER);
        }
        pageViewModel.setIndex(index);
    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View root = inflater.inflate(R.layout.fragment_main, container, false);
        final TextView textView = root.findViewById(R.id.section_label);
        pageViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        Button saveBtn = root.findViewById(R.id.savaBtn);
        EditText editText = root.findViewById(R.id.setname);
        editText.setHint(Chessboard.name);
        int[] tmp = {R.id.Photo1,R.id.Photo2,R.id.Photo3};
        RadioButton radioButton = root.findViewById(tmp[Chessboard.photo-1]);
        radioButton.setChecked(true);
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = root.findViewById(R.id.setname);
                Chessboard.name = editText.getText().toString();
                try {
                    FileOutputStream fileOutputStream = root.getContext().openFileOutput("config", Context.MODE_PRIVATE);
                    fileOutputStream.write((Chessboard.name+"#"+String.valueOf(Chessboard.photo)).getBytes());
                    if(fileOutputStream!=null){
                        fileOutputStream.close();
                        Toast.makeText(root.getContext(),Chessboard.name+" 已保存，开始游戏吧",Toast.LENGTH_LONG).show();
                    }
                }catch (Exception e){
                    Log.i("ss","s");
                }
            }
        });

        RadioGroup radioGroup = root.findViewById(R.id.setPhoto);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.Photo1:
                        Chessboard.photo = 1;
                        break;
                    case R.id.Photo2:
                        Chessboard.photo = 2;
                        break;
                    case R.id.Photo3:
                        Chessboard.photo = 3;
                        break;
                }
            }
        });
        return root;
    }
}